package test07;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class Myrunable implements Runnable {
    Socket socket;

    public Myrunable(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            String s = UUID.randomUUID().toString().replace("-", "");
            //接收数据
            BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream());
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                    new FileOutputStream("D:\\27162\\Desktop\\itheima\\untitled\\" + s + ".jpg")
            );
            int len;
            byte[] bytes = new byte[1024];
            while ((len = bufferedInputStream.read(bytes)) != -1) {
                bufferedOutputStream.write(bytes, 0, len);
            }
            bufferedOutputStream.flush();
            //回显
            BufferedWriter bufferedWriter = new BufferedWriter(
                    new OutputStreamWriter(socket.getOutputStream())
            );
            bufferedWriter.write("上传成功");
            bufferedWriter.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //断开连接
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
